package com.order.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.order.dao.ICustomerDao;
import com.order.domain.Customer;
import com.order.domain.User;


/**
 * 学生的持久层实现类
 * @author zhy
 *
 */
@Repository("customerDao")
public class CustomerDaoImpl implements ICustomerDao {
	
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> findAll(DetachedCriteria dCriteria) {
		return (List<Customer>) hibernateTemplate.findByCriteria(dCriteria);
	}

	@Override
	public void save(Customer customer) {
		hibernateTemplate.save(customer);
	}

	@Override
	public Customer findById(Integer studentId) {
		Customer c = hibernateTemplate.load(Customer.class, studentId);
		//System.out.println("持久层执行根据id查询学生后的对象："+c.hashCode());
		return c;
	}
	
	@Override
	public void delete(Integer studentId) {
		hibernateTemplate.delete(findById(studentId));
	}

	@Override
	public void update(Customer customer) {
		hibernateTemplate.update(customer);
	}

	/**
	 * 查询总记录数（今日还未联系人数）
	 * 返回一个结果
	 * select count(*) from Customer where 条件
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int findRecord(DetachedCriteria dCriteria) {
		List<Object> findByCriteria = (List<Object>) hibernateTemplate.findByCriteria(dCriteria);
		if(findByCriteria!=null && findByCriteria.size()>0){
			return Integer.valueOf(findByCriteria.get(0).toString());
		}
		return 0;
	}

	/**
	 * 查询分页数据
	 * 返回List<Customer>
	 * select * from Customer  where 条件
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> findData(DetachedCriteria dCriteria, int startIndex, int pageSize) {
		return (List<Customer>) hibernateTemplate.findByCriteria(dCriteria, startIndex, pageSize);
	}

	/**
	 * 拉出公海
	 */
	@Override
	public void updatePull(Integer studentId) {
		
		Customer customer = hibernateTemplate.get(Customer.class, studentId);
		
		User u = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		
		customer.setAffiliatedTeacher(u);
		
		hibernateTemplate.update(customer);
		
	}

	/**
	 * 查询学生数量
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Integer findStudentNum(DetachedCriteria dCriteria) {
		List<Object> findByCriteria = (List<Object>) hibernateTemplate.findByCriteria(dCriteria);
		if(findByCriteria!=null && findByCriteria.size()>0){
			return Integer.valueOf(findByCriteria.get(0).toString());
		}
		return 0;
	}

	/**
	 * 拉入公海
	 */
	@Override
	public void updatePut(Integer studentId) {
		Customer customer = hibernateTemplate.get(Customer.class, studentId);
		
		User u = new User();
		u.setUserId(0);
	
		customer.setAffiliatedTeacher(u);
		customer.setNextDate("");
		hibernateTemplate.update(customer);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void deleteAll(int checkTnum) {
		hibernateTemplate.delete(findById(checkTnum));
	}

	/**
	 * 查询每个员工未联系学生人数
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int findContactNum(DetachedCriteria ddCriteria) {
		List<Object> findByCriteria = (List<Object>) hibernateTemplate.findByCriteria(ddCriteria);
		if(findByCriteria!=null && findByCriteria.size()>0){
			return Integer.valueOf(findByCriteria.get(0).toString());
		}
		return 0;
	}

	/**
	 * 删除员工时，所属学生全部归为“公海”
	 */
	@Override
	public void updateAffiliatedTeache(Integer studentId) {
		Customer customer = hibernateTemplate.get(Customer.class, studentId);
		User u = hibernateTemplate.get(User.class, 0);
		
		customer.setAffiliatedTeacher(u);
		customer.setNextDate("");
		hibernateTemplate.update(customer);
	}
}
